<?php
namespace App\DBs;

use Illuminate\Support\Facades\DB;

class MemberDB extends BaseDB
{
    /**
     * 用户
     */

    public $tUser = 'user';
    public $tFriend = 'u_friend';
    public $tActivity = 'activity';

    public function __construct()
    {
    }

    /**
     * 用户列表
     */
    public function getIndex()
    {
        $models = DB::table($this->tUser)->where('del',0)->get();
        if (!count($models)) {
            return array('code'=>'-1','msg'=>'no data');
        }
        $items = array();
        foreach ($models as $model) {
            $items[] = [
//                'id'=>$model->id, 'uname'=>$model->uname,
                'phone'=>$model->phone, 'wx'=>$model->wx, 'xiaoqu'=>$model->village,
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }

    /**
     * 通过手机号码，查询用户
     */
    public function getOneByPhone($phone)
    {
        return DB::table($this->tUser)->where('phone',$phone)->first();
    }

    /**
     * 通过手机号码，查询用户
     */
    public function getOneByWx($wx)
    {
        return DB::table($this->tUser)->where('wx',$wx)->first();
    }

    /**
     * 新增用户
     */
    public function setAdd($phone,$wx,$xiaoqu)
    {
        return DB::table($this->tUser)->insertGetId([
            'uname'=>'u'.time().rand(100,999),
            'phone'=>$phone, 'wx'=>$wx, 'village'=>$xiaoqu, 'created_at'=>time(),
        ]);
    }

    /**
     * 好友列表
     */
    public function getFriendIndex($uwx)
    {
        $items[] = [
            'id'=>1, 'uwx'=>'uwx',
            'fwx'=>'fwx', 'fhead'=>'/imgs/logo.png', 'fphone'=>'11111111111',
        ];
        return array('code'=>'0','msg'=>'success','data'=>$items);
        $models = DB::table($this->tFriend)->where('del',0)->where('uwx',$uwx)
            ->skip(0)->take(10)->orderBy('id','desc')->get();
        if (!count($models)) { return array('code'=>'-1','msg'=>'no data'); }
        $items = array(); $models = $this->jsonToArr($models);
        foreach ($models as $model) {
            $userOnes = DB::table($this->tUser)->where('wx',$model['fwx'])->get();
            $userOne = count($userOnes) ? $this->jsonToArr($userOnes[0]) : '';
            $fphone = $userOne ? $userOne['phone'] : '';
            $fhead = $userOne ? $userOne['head'] : '';
            $items[] = [
                'id'=>$model['id'], 'uwx'=>$model['uwx'],
                'fwx'=>$model['fwx'], 'fhead'=>$fhead, 'fphone'=>$fphone,
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }

    /**
     * 抽奖次数
     */
    public function getRewardsTotal($uwx)
    {
        $models = DB::table($this->tUser)->where('del',0)->where('wx',$uwx)
            ->skip(0)->take(1)->orderBy('id','desc')->get();
        if (!count($models)) { return array('code'=>'-1','msg'=>'no data'); }
        $model = $this->jsonToArr($models[0]);
        $rewardNum = 0;
        if (intval($model['jifen']/100)) { $rewardNum += intval($model['jifen']/100); }
        if (intval($model['money']/100)) { $rewardNum += intval($model['money']/100); }
        if (intval($model['buyNum']/10)) { $rewardNum += intval($model['buyNum']/10); }
        $items = [
            'jifen'=>$model['jifen'], 'money'=>$model['money'], 'buyNum'=>$model['buyNum'],
        ];
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }

    /**
     * 活动列表
     */
    public function getActivityIndex()
    {
        $models = DB::table($this->tActivity)->where('del',0)
            ->skip(0)->take(100)->orderBy('id','asc')->get();
        if (!count($models)) { return array('code'=>'-1','msg'=>'no data'); }
        $items = array(); $models = $this->jsonToArr($models);
        $keys = ['一','二','三','四','五','六','七','八','九','十',];
        foreach ($models as $k => $model) {
            $kname = array_key_exists($k, $keys) ? $keys[$k] : '';
            $items[] = [
                'key'=>$kname, 'detail'=>$model['detail'],
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }
}